package com.pengbei.enshrine.ui.account;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.google.gson.Gson;
import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallbackProxy;
import com.pengbei.enshrine.R;
import com.pengbei.enshrine.base.BaseFragment;
import com.pengbei.enshrine.common.utils.PreferencesUtils;
import com.pengbei.enshrine.common.utils.StatusBarUtils;
import com.pengbei.enshrine.network.api.SearchAuthorApi;
import com.pengbei.enshrine.network.http.HttpData;

import java.util.List;

public class AccountFragment extends BaseFragment {

    private TextView tv_test;

    @Override
    public void onResume() {
        super.onResume();
        if (!isValid()){
            return;
        }
        StatusBarUtils.StatusBarTransparentBgBlackText(mActivity);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_account, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initview(view);
        refreshData();
    }

    private void initview(View view){
        tv_test = view.findViewById(R.id.tv_test);
    }

    public void refreshData() {
        EasyHttp.get(this)
                .api(new SearchAuthorApi().setId(190000))
                .request(new HttpCallbackProxy<HttpData<List<SearchAuthorApi.Bean>>>(this) {
                    @Override
                    public void onHttpSuccess(HttpData<List<SearchAuthorApi.Bean>> result) {
                        String test = new Gson().toJson(result);
                        tv_test.setText(test);
                    }
                });
    }

    private void exit(){
        PreferencesUtils.putString(mActivity,"","");
    }

    private AppCompatActivity mActivity;

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        mActivity = (AppCompatActivity) context;
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mActivity = null;  // 清理Activity引用
    }

    private boolean isValid(){
        boolean isValid = true;
        if(null==mActivity||mActivity.isFinishing()||mActivity.isDestroyed()){
            isValid = false;
        }
        return isValid;
    }


}
